package cn.bx.bid.ctrl;

import cn.bx.bid.entity.*;
import cn.bx.bid.service.ProfessorService;
import cn.bx.bid.util.DocUtil;
import cn.bx.bid.util.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProfessorController  {

    //配置下载路径
   @Value("${uploadPath}")
    private String uploadPath;

    @Resource
    ProfessorService professorService;

    @RequestMapping(value = "professorSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Professor> professorSearch(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam("limit")int limit, @RequestParam(value = "name",required = false)String name, @RequestParam(value = "tele",required = false)String tele, @RequestParam(value = "idcard",required = false)String idcard){
        page--;

        org.springframework.data.domain.Page<Professor> p= professorService.paging(page,limit,name,tele,idcard);
        return  Page.toPage(p);

    }

    //增加专家

    @RequestMapping(value = "professorAdd",produces = "application/json;charset=utf-8")
    public AjaxResult professorAdd(@ModelAttribute("p") Professor professor){
       int a= professorService.addprofessor(professor);
       return a>0? AjaxResult.success("增加成功"): AjaxResult.error("增加失败");
    }

    //查询专家
    @GetMapping(value = "searchoneperson",produces = "application/json;charset=utf-8")
    public AjaxResult search(@RequestParam("id") long id){
       Professor p= professorService.get(id);
       if(p!=null){
           return AjaxResult.success("true",p);
       }
        return AjaxResult.error("false");
    }

//专家工作历史
    @GetMapping(value = "getworkhistory",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult professorGet(@RequestParam("id") long id){
       List<WorkHistory> list=professorService.getHistory(id);

        if(list!=null){
            return AjaxResult.success("true",list);
        }
        return AjaxResult.error("false");
    }
//    jionproject-del", {
//    projid:data.id,profid:id
@RequestMapping(value = "jionproject-del",produces = "application/json;charset=utf-8")
 public AjaxResult jionprojectDel(@RequestParam("projid")long projid, @RequestParam("profid")long profid){
    int a=professorService.jionprosdel(projid,profid);
    return a>0? AjaxResult.success("删除成功"): AjaxResult.error("删除失败");
}

    @RequestMapping(value = "getprojects",produces = "application/json;charset=utf-8")
    public AjaxResult getprojects(@RequestParam("id") long professorId){
        LogUtil.info("professorid>>>>>>>>>>>"+professorId);
        List<RandomName> list =professorService.getprojects(professorId);
        return AjaxResult.success("true",list);
   }
    // jionprojectAdd
    @RequestMapping(value = "jionprojectAdd",produces = "application/json;charset=utf-8")
    public AjaxResult jionprojectAdd(@ModelAttribute("p") RandomName randomName){
        int a= professorService.jionprojectAdd(randomName);
        return a>0? AjaxResult.success("增加成功"): AjaxResult.error("增加失败");
    }
   //增加工作历史  historyAdd
    @RequestMapping(value = "historyAdd",produces = "application/json;charset=utf-8")
    public AjaxResult historyAdd(@ModelAttribute("p") WorkHistory workHistory){
        int a= professorService.addHistory(workHistory);
        return a>0? AjaxResult.success("增加成功"): AjaxResult.error("增加失败");
    }

    //获得该工作历史   workhistoryGet
    @RequestMapping(value = "workhistoryGet",produces = "application/json;charset=utf-8")
    public AjaxResult workhistoryGet(@RequestParam("id")long id){
   WorkHistory workHistory= professorService.getOneHistory(id);
   if(workHistory!=null){
       return AjaxResult.success("查询成功",workHistory);
   }
   return  AjaxResult.error("没有改记录");
    }
    //修改工作历史  historyAdd
    @RequestMapping(value = "historyEdit",produces = "application/json;charset=utf-8")
    public AjaxResult historyEdit(@ModelAttribute("p") WorkHistory workHistory){
        int a= professorService.historyupdate(workHistory);
        return a>0? AjaxResult.success("修改成功"): AjaxResult.error("修改失败");
    }
  //删除工作历史  workhistoryDel
  @RequestMapping(value = "workhistoryDel",produces = "application/json;charset=utf-8")
  public AjaxResult workhistoryDel(@RequestParam("hisid")long hisid, @RequestParam("id")long pid){
      int a= professorService.delHis(pid,hisid);
      return a>0? AjaxResult.success("删除成功"): AjaxResult.error("删除失败");
  }
    //修改专家
    @RequestMapping(value = "professorUpdata",produces = "application/json;charset=utf-8")
   public AjaxResult professorUpdata(@ModelAttribute("p") Professor professor){
      //  professorService.
        System.out.println("修改专家："+professor);
      int a= professorService.updataprofessor(professor);
      if(a>0){
          return  AjaxResult.success("修改成功");
      }
        return  AjaxResult.error("修改失败");
    }

    //删除专家
    @RequestMapping(value = "professordel",produces ="application/json;charset=utf-8" )
    public AjaxResult professordel(@RequestParam("id") Long id){
       int a= professorService.delprofessor(id);
        if(a>0){
            return  AjaxResult.success("删除成功");
        }

        return AjaxResult.error("上除失败");
    }
    //批量删除
    @RequestMapping(value = "batchDelprofessor",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids[]")long[] ids){
        int b=  professorService.batchDel(ids);
        return b>0? AjaxResult.success("删除成功"): AjaxResult.error("删除失败");
    }

    //导出word
    @RequestMapping(value = "exportProToWordProf",produces = "application/json;charset=utf-8")
    @ResponseBody
    public void  exportProToWordProf(@RequestParam("id")long id,HttpServletResponse response)throws ServletException, IOException  {

        Professor p = professorService.get(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("p", p);
        String path =uploadPath+ URLEncoder.encode(p.getProfessorName()+ ".doc", "UTF-8");
        DocUtil.exportProfessor(path, map);
        File file = new File(path);
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(p.getProfessorName()+ ".doc", "UTF-8"));
        FileInputStream fin = new FileInputStream(file);
        byte[] b = new byte[fin.available()];
        fin.read(b);
        fin.close();
        ServletOutputStream out = response.getOutputStream();
        out.write(b);
        out.flush();
        file.delete();//下载后删除文件
    }
    //异常处理
    @ExceptionHandler
    public AjaxResult error(Exception e){
        return  AjaxResult.error("false",e);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

}
